﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lamex.Library.UserCotrolUI;
using DevExpress.XtraEditors;
using Lamex.Base;
using Lamex.Common;
using AxWMPLib;
using System.Data;
using System.Windows.Forms;

namespace Lamex.mMES.Common
{
    public class LampHelper
    {
        //信号灯颜色变化
        public static void ChargeLamp(string BarCode, string UserName, LabelControl lb_Name1, LabelControl lb_Name2, LabelControl LabelControl1, PictureBox picTop1, PictureBox picTop2, PictureBox picTop3,
            LabelControl lblInfo_4, LabelControl lblStatus, FrmLampControlUI LampGreen, LabelControl lblInfo_1, LabelControl lblInfo_2, LabelControl lblInfo_3, Timer timer)
        {
            if (BarCode.Trim().Length > 6)
            {
                DataTable dtWorkCode = Common.GetDataTable.GetWork_Barcode();
                if (BarCode.Substring(0, 6) == "MES_PD")
                {
                    //开启信号灯变化
                    StopLamp(LampGreen);
                    StartLamp("Green", lblStatus, LampGreen);
                    StartTime(timer);
                    Common.ShowHelper.ShowLblInfo(lblInfo_1, lblInfo_2, lblInfo_3, lblInfo_4);
                    RefreshList.InitLoad(UserName, lb_Name1, lb_Name2, LabelControl1, picTop1, picTop2, picTop3, BarCode);//2020-1-16
                    lblInfo_4.Text = "产品码扫描成功！";


                }
                else
                {
                    if (dtWorkCode.Select("BARCODE_SN='" + BarCode + "'").Length > 0)
                    {
                        string strColor = dtWorkCode.Select("BARCODE_SN='" + BarCode + "'").CopyToDataTable().Rows[0]["LIGHT_STATE"].ToString();
                        string strDesc = dtWorkCode.Select("BARCODE_SN='" + BarCode + "'").CopyToDataTable().Rows[0]["BARCODE_DESC"].ToString();
                        //开启信号灯变化
                        StopLamp(LampGreen);
                        StartLamp(strColor, lblStatus, LampGreen);

                        if (strColor == "Green")
                        {
                            StartTime(timer);
                            RefreshList.InitLoad(UserName, lb_Name1, lb_Name2, LabelControl1, picTop1, picTop2, picTop3, BarCode);//2020-1-16
                        }
                        else
                            endTime(timer);
                        Common.ShowHelper.ShowLblInfo(lblInfo_1, lblInfo_2, lblInfo_3, lblInfo_4);
                        lblInfo_4.Text = string.IsNullOrEmpty(strDesc) ? BarCode : strDesc;

                    }
                }
            }
        }
        //开启信号灯
        public static void StartLamp(string Color, LabelControl lblStatus, FrmLampControlUI LampGreen)
        {
            switch (Color)
            {
                case "Green":
                    LampGreen.LampColor = new System.Drawing.Color[] {
        System.Drawing.Color.Green,
        System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))))
                        };
                    LampGreen.TwinkleSpeed = 100;
                    lblStatus.Text = "正常生产";
                    break;
                case "Red":

                    LampGreen.LampColor = new System.Drawing.Color[] {
                System.Drawing.Color.Red,
        System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))))
                        };
                    LampGreen.TwinkleSpeed = 100;
                    lblStatus.Text = "暂停生产";
                    break;
                case "Yellow":
                    LampGreen.LampColor = new System.Drawing.Color[] {
        System.Drawing.Color.Yellow,
        System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))))
                    };
                    LampGreen.TwinkleSpeed = 100;
                    lblStatus.Text = "缺料等待";
                    break;
                case "Blue":
                    LampGreen.LampColor = new System.Drawing.Color[] {
        System.Drawing.Color.Blue,
        System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))))
                    };
                    LampGreen.TwinkleSpeed = 100;
                    lblStatus.Text = "准备生产";
                    break;
            }
        }
        //停止信号灯
        public static void StopLamp(FrmLampControlUI LampGreen)
        {
            LampGreen.LampColor = new System.Drawing.Color[] { System.Drawing.Color.Silver, System.Drawing.Color.Silver };
            LampGreen.TwinkleSpeed = 0;
        }

        public static void StartTime(Timer timer1)
        {

            timer1.Enabled = true;
            timer1.Interval = 1000 * 30; //执行间隔时间,单位为毫秒; 
            timer1.Start();
         
            //t4 = new System.Timers.Timer(5000 * 4);
            //t4.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp1);
            //t4.AutoReset = true;
            //t4.Enabled = true;
            //t4.Start();

        }
        //关闭定时器
        public static void endTime(Timer timer1)
        {
            timer1.Enabled = false;
            timer1.Stop();
        }
        //public void Timer_TimesUp1(System.Timers.Timer t4, System.Timers.Timer t2, PanelControl pcBottom)
        //{
        //    try
        //    {
        //        if (t4 != null)
        //        {
        //            t4.Stop();
        //            t4.Dispose();
        //            t4 = null;
        //        }
        //        RefreshList.IsPicVisible(pcBottom);
        //        if (t2 == null)
        //        {
        //            t2 = new System.Timers.Timer(5000 * 4);
        //            t2.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp2);
        //            t2.AutoReset = true;
        //            t2.Enabled = true;
        //            t2.Start();
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message + "123");
        //    }
        //}
        //public void Timer_TimesUp2(System.Timers.Timer t4, System.Timers.Timer t2, PanelControl pcBottom)
        //{
        //    try
        //    {
        //        if (t2 != null)
        //        {
        //            t2.Stop();
        //            t2.Dispose();
        //            t2 = null;
        //        }
        //        RefreshList.IsPieVisible(pcBottom);
        //        if (t4 == null)
        //        {
        //            t4 = new System.Timers.Timer(5000 * 2);
        //            t4.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp1(t4, t2, pcBottom));
        //            t4.AutoReset = true;
        //            t4.Enabled = true;
        //            t4.Start();
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message + "124");
        //    }
        //}
    }
}
